<template>
	<view class="page">
		<nav-bar :backState="module.shopCarPage ? '1000' : '2000'" title="购物车">
			<view class="right_manage" slot="right" @click="onDeleteState">{{isDelete ? '取消':'管理'}}</view>
		</nav-bar>
        <!-- 公共组件-每个页面必须引入 -->
        <public-module></public-module>
		<view class="shop_list" v-for="(item, index) of shopCarList" :key="item.objId">
			<view class="shop_title" v-if="module.shop">
				<text class="icon_select" :class="{ active: item.checked }" @click="onShopAllSelect(index)"></text>
				<view @click="onPageJump('/pages/home/shop/shop?objId=' + item.objId)">{{ item.name }}</view>
			</view>
			<view class="goods_list" v-for="(childItem, childIndex) of item.goods" :key="childItem.objId">
				<text class="icon_select" :class="{ active: childItem.checked }" @click="onSelectGoods(index, childIndex)"></text>
				<image :src="childItem.img" mode="aspectFill" @click="onPageJump('/pages/home/shop/goodsDetail?objId=' + childItem.objId)"></image>
				<view class="goods_info">
					<view class="name" @click="onPageJump('/pages/home/shop/goodsDetail?objId=' + childItem.objId)">{{ childItem.name }}</view>
					<view class="specification">规格: {{ childItem.goodsSpecName }}</view>
					<view class="price_num_box">
						<text class="price">{{ childItem.pricePay }}</text>
						<view class="num_box">
							<view :class="{ active: childItem.goodsNum > 1 }" @click="onNumLess(index, childIndex)">-</view>
							<text>{{ childItem.goodsNum }}</text>
							<view :class="{ active: childItem.goodsNum < childItem.stock }" @click="onNumPlus(index, childIndex)">+</view>
						</view>
					</view>
				</view>
			</view>
			<view class="shop_btn_box">
				<view class="select_price_box">
					<view class="shop_all_select" v-if="!module.shop" @click="onShopAllSelect(index)">
						<text class="icon_all_select" :class="{ active: item.checked }"></text>
						全选
					</view>
					<view class="total_price">
						合计：
						<text>{{ item.priceTotal }}</text>
					</view>
				</view>
				<view class="btn_box">
					<button type="primary" v-if="isDelete" @click="removeShopCat(index)">删除</button>
					<button type="primary" v-else class="active" @click="onGoBuy(index)">去结算</button>
				</view>
			</view>
		</view>
		<view class="no_data" v-if="shopCarList.length == 0">
			<image src="../../static/icon/ic_shoppingcart_empty.png" mode="aspectFit"></image>
			<text>你的购物车空空如也~</text>
			<button @click="onJumpHome">去购物</button>
		</view>
		<recommend-goods ref="recommend"></recommend-goods>
	</view> 
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex';
	import recommendGoods from '@/components/module/recommend_goods';
	export default {
		components: {
			recommendGoods
		},
		data() {
			return {
				shopCarList: [],
				// 第一页
				pageNo: 1,
				// 总页数默认第一页
				pages: 1,
				isDelete: false,
				module: {}
			};
		},
		//第一次加载
		onLoad(e) {
			this.module = this.$base.module;
		},
		//页面显示
		onShow() {
			this.judgeLogin(() => {
				this.pageNo = 1;
				this.pages = 1;
				this.loadData();
			});
		},
		//方法
		methods: {
			...mapMutations(['setPreOrderData']),
			onDeleteState() {
				this.isDelete = !this.isDelete;
			},
			onJumpHome(){
				uni.switchTab({
					url: this.$base.homePath
				});
			},
			loadData() {
				if (this.pageNo <= this.pages) {
					var httpData = {
						pageNo: this.pageNo,
						pageSize: 15
					};
					this.$http
						.post('api/mall/cart/shop_goods/v1/list', httpData, {
							load: false
						})
						.then(res => {
							uni.stopPullDownRefresh();
							this.pages = res.pages;
							res.data.map(item => {
								item.checked = true;
								item.goods.map(childItem => {
									childItem.checked = true;
									return childItem;
								});
								return item;
							});
							if (this.pageNo == 1) {
								this.shopCarList = res.data;
							} else {
								this.shopCarList = this.shopCarList.concat(res.data);
							}
							if (res.data.length > 0) {
								this.pageNo = this.pageNo + 1;
							}
						});
				}
			},
			//页面跳转
			onPageJump(url) {
				uni.navigateTo({
					url: url
				});
			},
			//店铺全选
			onShopAllSelect(index) {
				let item = this.shopCarList[index];
				item.goods.forEach(childItem => {
					if (item.checked) {
						childItem.checked = false;
					} else {
						childItem.checked = true;
					}
				});
				if (item.checked) {
					this.shopCarList[index].checked = false;
					this.shopCarList[index].priceTotal = '￥0';
				} else {
					let ids = this.getCheckedGoods(index);
					this.getPriceTotal(index, ids);
				}
			},
			//商品选择
			onSelectGoods(index, childIndex) {
				this.shopCarList[index].goods[childIndex].checked = !this.shopCarList[index].goods[childIndex].checked;
				let ids = this.getCheckedGoods(index);
				this.getPriceTotal(index, ids);
			},
			//查询选中id
			getCheckedGoods(index) {
				let ids = [];
				let allSelect = true;
				this.shopCarList[index].goods.forEach(item => {
					if (item.checked) {
						ids.push({
							goodsId: item.objId,
							goodsSpecId: item.goodsSpecId,
							goodsNum: item.goodsNum
						});
					} else {
						allSelect = false;
					}
				});
				this.shopCarList[index].checked = allSelect;
				return ids;
			},
			//总金额查询
			getPriceTotal(index, goods) {
				this.$http
					.post('api/mall/cart/shop_goods/v1/price_total', {
						shopId: this.shopCarList[index].objId,
						goods: goods
					})
					.then(res => {
						this.shopCarList[index].priceTotal = res;
					});
			},
			//商品数量减
			onNumLess(index, childIndex) {
				let goodsNum = this.shopCarList[index].goods[childIndex].goodsNum;
				if (goodsNum > 1) {
					this.shopCarList[index].goods[childIndex].goodsNum -= 1;
					let ids = this.getCheckedGoods(index);
					this.$http
						.post('api/mall/cart/shop_goods/v1/add_minus', {
							shopId: this.shopCarList[index].objId,
							goodsId: this.shopCarList[index].goods[childIndex].objId,
							goodsSpecId: this.shopCarList[index].goods[childIndex].goodsSpecId,
							goodsNum: this.shopCarList[index].goods[childIndex].goodsNum
						})
						.then(res => {
							this.getPriceTotal(index, ids);
						});
				}
			},
			//商品数量加
			onNumPlus(index, childIndex) {
				let goodsNum = this.shopCarList[index].goods[childIndex].goodsNum;
				let stock = this.shopCarList[index].goods[childIndex].stock;
				if (goodsNum < stock) {
					this.shopCarList[index].goods[childIndex].goodsNum += 1;
					let ids = this.getCheckedGoods(index);
					this.$http
						.post('api/mall/cart/shop_goods/v1/add_minus', {
							shopId: this.shopCarList[index].objId,
							goodsId: this.shopCarList[index].goods[childIndex].objId,
							goodsSpecId: this.shopCarList[index].goods[childIndex].goodsSpecId,
							goodsNum: this.shopCarList[index].goods[childIndex].goodsNum
						})
						.then(res => {
							this.getPriceTotal(index, ids);
						});
				}
			},
			//删除购物车
			removeShopCat(index) {
				let shopGoods = this.getCheckedGoods(index);
				this.$http
					.post('api/mall/cart/shop_goods/v1/del', {
						shopGoods: [{
							shopId: this.shopCarList[index].objId,
							goods: shopGoods
						}]
					})
					.then(res => {
						uni.showToast({
							title: '删除成功！',
							icon: 'none'
						});
						this.isDelete = false;
						this.pageNo = 1;
						this.pages = 1;
						this.loadData();
					});
			},
			onGoBuy(index) {
				const item = this.shopCarList[index];
				let shopGoods = this.getCheckedGoods(index);
				this.setPreOrderData({
					goods: shopGoods,
					shopId: item.objId,
					shoppingCart: true,
					goodsPackage: false
				});
				uni.navigateTo({
					url: '/pages/shopCar/confirmOrder'
				});
			}
		},

		//页面隐藏
		onHide() {},
		//页面卸载
		onUnload() {},
		//页面下来刷新
		onPullDownRefresh() {
			this.pageNo = 1;
			this.pages = 1;
			this.loadData();
		},
		//页面上拉触底
		onReachBottom() {
			this.$refs.recommend.loadMore();
		},
		//用户点击分享
		onShareAppMessage(e) {
			return this.wxShare();
		}
	};
</script>
<style lang="scss" scoped>
	@import '@/style/mixin.scss';

	.page {
		padding-top: 20rpx;
	}

	.right_manage {
		padding-right: 30rpx;
		font-size: 28rpx;
		color: #333333;
	}
	.no_data {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 430rpx;
		image {
			width: 176rpx;
			height: 129rpx;
		}
		text {
			font-size: 32rpx;
			color: #333435;
			margin-top: 30rpx;
		}
		button {
			width: 160rpx;
			height: 60rpx;
			border-radius: 8rpx;
			border: solid 2rpx #e5e5e5;
			margin-top: 30rpx;
			font-size: 28rpx;
			color: #333435;
			line-height: 60rpx;
			background-color: transparent;
		}
	}
	.shop_list {
		margin-bottom: 20rpx;
		background-color: #fff;

		.icon_select {
			@include theme('unselected_img', '../../');
			background-repeat: no-repeat;
			background-position: center center;
			background-size: cover;
			width: 38rpx;
			height: 38rpx;
			margin-right: 30rpx;
			flex-shrink: 0;

			&.active {
				@include theme('check_img', '../../');
			}
		}

		.shop_title {
			display: flex;
			align-items: center;
			padding: 30rpx;

			>view {
				display: flex;
				align-items: center;
				font-size: 32rpx;
				color: #333333;

				&::after {
					content: '';
					@include bis('../../static/icon/me_lise_more.png', 100% 100%);
					width: 12rpx;
					height: 22rpx;
					margin-left: 20rpx;
				}
			}
		}

		.goods_list {
			display: flex;
			align-items: center;
			padding: 30rpx;
			border-bottom: 1rpx solid #f8f8f8;

			>image {
				width: 200rpx;
				height: 200rpx;
				border-radius: 10rpx;
				flex-shrink: 0;
			}

			.goods_info {
				margin-left: 30rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				min-height: 200rpx;
				width: calc(100% - 298rpx);

				.name {
					font-size: 32rpx;
					color: #333333;
					@include bov(2);
				}

				.specification {
					font-size: 24rpx;
					color: #666666;
				}

				.price_num_box {
					display: flex;
					justify-content: space-between;
					align-items: center;

					.price {
						font-size: 30rpx;
						color: #e84134;
					}

					.num_box {
						display: flex;

						view {
							width: 60rpx;
							height: 60rpx;
							background-color: #f7f7f7;
							color: #cccccc;
							text-align: center;
							font-size: 28rpx;
							line-height: 60rpx;

							&:first-child {
								border-radius: 8rpx 0rpx 0rpx 8rpx;
							}

							&:last-child {
								border-radius: 0rpx 8rpx 8rpx 0rpx;
							}

							&.active {
								background-color: #e5e5e5;
								color: #999999;
							}
						}

						text {
							width: 60rpx;
							height: 60rpx;
							line-height: 60rpx;
							font-size: 28rpx;
							font-weight: bold;
							color: #555;
							text-align: center;
							background-color: #e5e5e5;
							margin: 0 2rpx;
						}
					}
				}
			}
		}

		.shop_btn_box {
			display: flex;
			padding: 30rpx;
			align-items: center;
			justify-content: space-between;
			padding-left: 30rpx;
			.select_price_box {
				display: flex;
				align-items: center;
				.shop_all_select {
					display: flex;
					align-items: center;
					font-size: 28rpx;
					color: #333333;
					.icon_all_select {
						@include theme('unselected_img', '../../');
						background-repeat: no-repeat;
						background-position: center center;
						background-size: cover;
						width: 38rpx;
						height: 38rpx;
						flex-shrink: 0;
						margin-right: 24rpx;
						
						&.active {
							@include theme('check_img', '../../');
						}
					}
				}
				.total_price {
					font-size: 28rpx;
					color: #333333;
					margin-left: 60rpx;
					text {
						font-size: 30rpx;
						color: #e84134;
					}
				}
			}
			.btn_box {
				display: flex;

				button {
					width: 150rpx;
					height: 56rpx;
					line-height: 52rpx;
					border: 2rpx solid $themeColor;
					background-color: transparent;
					border-radius: 33rpx;
					font-size: 28rpx;
					color: $themeColor;
					margin-left: 30rpx;

					&.active {
						background-color: $themeColor;
						color: #ffffff;
						border: 0;
					}
				}
			}
		}
	}
</style>
